home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / atom / url.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.0 KB  |  146 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __author__ = 'api.jscudder (Jeff Scudder)'
  5. import urlparse
  6. import urllib
  7. DEFAULT_PROTOCOL = 'http'
  8. DEFAULT_PORT = 80
  9.  
  10. def parse_url(url_string):
  11.     '''Creates a Url object which corresponds to the URL string.
  12.   
  13.   This method can accept partial URLs, but it will leave missing
  14.   members of the Url unset.
  15.   '''
  16.     parts = urlparse.urlparse(url_string)
  17.     url = Url()
  18.     if parts[0]:
  19.         url.protocol = parts[0]
  20.     
  21.     if parts[1]:
  22.         host_parts = parts[1].split(':')
  23.         if host_parts[0]:
  24.             url.host = host_parts[0]
  25.         
  26.         if len(host_parts) > 1:
  27.             url.port = host_parts[1]
  28.         
  29.     
  30.     if parts[2]:
  31.         url.path = parts[2]
  32.     
  33.     if parts[4]:
  34.         param_pairs = parts[4].split('&')
  35.         for pair in param_pairs:
  36.             pair_parts = pair.split('=')
  37.             if len(pair_parts) > 1:
  38.                 url.params[urllib.unquote_plus(pair_parts[0])] = urllib.unquote_plus(pair_parts[1])
  39.                 continue
  40.             if len(pair_parts) == 1:
  41.                 url.params[urllib.unquote_plus(pair_parts[0])] = None
  42.                 continue
  43.         
  44.     
  45.     return url
  46.  
  47.  
  48. class Url(object):
  49.     '''Represents a URL and implements comparison logic.
  50.   
  51.   URL strings which are not identical can still be equivalent, so this object
  52.   provides a better interface for comparing and manipulating URLs than 
  53.   strings. URL parameters are represented as a dictionary of strings, and
  54.   defaults are used for the protocol (http) and port (80) if not provided.
  55.   '''
  56.     
  57.     def __init__(self, protocol = None, host = None, port = None, path = None, params = None):
  58.         self.protocol = protocol
  59.         self.host = host
  60.         self.port = port
  61.         self.path = path
  62.         if not params:
  63.             pass
  64.         self.params = { }
  65.  
  66.     
  67.     def to_string(self):
  68.         url_parts = [
  69.             '',
  70.             '',
  71.             '',
  72.             '',
  73.             '',
  74.             '']
  75.         if self.protocol:
  76.             url_parts[0] = self.protocol
  77.         
  78.         if self.host:
  79.             if self.port:
  80.                 url_parts[1] = ':'.join((self.host, str(self.port)))
  81.             else:
  82.                 url_parts[1] = self.host
  83.         
  84.         if self.path:
  85.             url_parts[2] = self.path
  86.         
  87.         if self.params:
  88.             url_parts[4] = self.get_param_string()
  89.         
  90.         return urlparse.urlunparse(url_parts)
  91.  
  92.     
  93.     def get_param_string(self):
  94.         param_pairs = []
  95.         for key, value in self.params.iteritems():
  96.             param_pairs.append('='.join((urllib.quote_plus(key), urllib.quote_plus(str(value)))))
  97.         
  98.         return '&'.join(param_pairs)
  99.  
  100.     
  101.     def get_request_uri(self):
  102.         '''Returns the path with the parameters escaped and appended.'''
  103.         param_string = self.get_param_string()
  104.         if param_string:
  105.             return '?'.join([
  106.                 self.path,
  107.                 param_string])
  108.         return self.path
  109.  
  110.     
  111.     def __cmp__(self, other):
  112.         if not isinstance(other, Url):
  113.             return cmp(self.to_string(), str(other))
  114.         difference = 0
  115.         if self.protocol and other.protocol:
  116.             difference = cmp(self.protocol, other.protocol)
  117.         elif self.protocol and not (other.protocol):
  118.             difference = cmp(self.protocol, DEFAULT_PROTOCOL)
  119.         elif not (self.protocol) and other.protocol:
  120.             difference = cmp(DEFAULT_PROTOCOL, other.protocol)
  121.         
  122.         if difference != 0:
  123.             return difference
  124.         difference = cmp(self.host, other.host)
  125.         if difference != 0:
  126.             return difference
  127.         if self.port and other.port:
  128.             difference = cmp(self.port, other.port)
  129.         elif self.port and not (other.port):
  130.             difference = cmp(self.port, DEFAULT_PORT)
  131.         elif not (self.port) and other.port:
  132.             difference = cmp(DEFAULT_PORT, other.port)
  133.         
  134.         if difference != 0:
  135.             return difference
  136.         difference = cmp(self.path, other.path)
  137.         if difference != 0:
  138.             return difference
  139.         return cmp(self.params, other.params)
  140.  
  141.     
  142.     def __str__(self):
  143.         return self.to_string()
  144.  
  145.  
  146.